from flask import Flask, request, jsonify
from src.main.python.service.graph_service import GraphService
from src.main.python.utils.getPublicData import *
from src.main.python.utils.getAllData import *
import random

from flask_cors import CORS

# webController层
class GraphController:

    def __init__(self, debug=True, port=5050):
        self.graphService = GraphService()
        self.app = Flask(__name__)
        CORS(self.app, resources={r"/*": {"origins": "*"}},supports_credentials=True)
        self.setup_routes()
        self.app.debug = debug
        self.app.run(port=port)
        print("初始化完毕, 一起开启愉快的知识之旅吧!!!")

    # 设置flask路由路径
    def setup_routes(self):
        # 根据指定的name查询实体及其所有关系
        @self.app.route('/searchEntity', methods=['GET', 'POST'])
        def search_entity():
            entityName = request.args.get('entityName')
            json_data = self.graphService.query_entity(str(entityName))
            return jsonify(json_data)

        # 查询所有的实体及其关系
        @self.app.route('/getAllEntities', methods=['GET', 'POST'])
        def get_all_entities():
            json_data = self.graphService.query_all_entity()
            return json_data

        # 查询实体模型
        @self.app.route('/getEntityModel', methods=['GET', 'POST'])
        def get_entity_model():
            json_data = self.graphService.query_entity_model()
            return json_data

        # 智能机器人问答
        @self.app.route('/chatbot', methods=['POST'])
        def chatbot():
            data = request.get_json()
            question = data.get('message', "你好")
            print(question)
            json_data = self.graphService.question_chatbot(question)
            return json_data

        # 知识图谱机器人问答
        @self.app.route('/KGQA_chatbot', methods=['POST'])
        def KGQA_chatbot():
            data = request.get_json()
            question = data.get('message', "你好")
            print(question)
            json_data = self.graphService.question_KGQA_chatbot(question)
            return json_data

        @self.app.route('/getHomeData', methods=['GET', 'POST'])
        def getHomeData():
            pieData = getPieData()
            configOne, wordDataOne = getConfigOne()
            configFive, wordDataFive = getConfigFive()
            casesData = list(getAllCasesData())
            maxNum, maxType, maxDep, maxHos, maxAge, minAge = getFoundData()
            boyList, girlList, ratioData = getGenderData()
            circleData = getCircleData()
            xData, y1Data, y2Data = getBodyData()
            return jsonify({
                'message': 'success',
                'code': 200,
                'data': {
                    'pieData': pieData,
                    'configOne': configOne,
                    'configFive': configFive,
                    'casesData': casesData,
                    'maxNum': maxNum,
                    'maxType': maxType,
                    'maxDep': maxDep,
                    'maxHos': maxHos,
                    'maxAge': maxAge,
                    'minAge': minAge,
                    'boyList': random.sample(boyList, 6),
                    'girlList': random.sample(girlList, 6),
                    'ratioData': ratioData,
                    'circleData': circleData,
                    'wordData': wordDataOne,
                    'lastData': {
                        'xData': xData[:10],
                        'y1Data': y1Data[:10],
                        'y2Data': y2Data[:10]
                    }
                }
            })


        @self.app.route('/tableData', methods=['GET', 'POST'])
        def tableData():
            tableDataList = getAllCasesData()
            resultData = [x[1:] for x in tableDataList]
            # print(resultData)
            return jsonify({
                'message': 'success',
                'code': 200,
                'data': {
                    'resultData': resultData
                }
            })

        @self.app.route('/medicalInfo', methods=['GET', 'POST'])
        def medicalInfo():
            medicalInfoList = list(getAllCasesData());
            # print(medicalInfoList)
            return jsonify({
                'message': 'success',
                'code': 200,
                'data': {
                    'medicalInfo': medicalInfoList[:20]
                }
            })



if __name__ == '__main__':
    controller: GraphController = GraphController()
    controller.app.debug = True
    controller.app.run()
